﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SksApp
{
    public partial class VergiTanimView : BaseView
    {
        public VergiTanimView()
        {
            InitializeComponent();
            this.Load += new EventHandler(VergiTanimView_Load);
        }

        void VergiTanimView_Load(object sender, EventArgs e)
        {
            using (SksEntity ent = new SksEntity())
            {
                var liste = ent.VergiOranlari.ToList();
                foreach (var item in liste)
                {
                    SetValue("txtNo" + item.No.ToString(), item.Sira.ToString());
                    SetValue("txtOran" + item.No.ToString(), item.Oran.ToString());
                    SetValue("txtTanim" + item.No.ToString(), item.Aciklama.ToString());
                }
            }
        }
        private void btnKapat_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Event.Invoke(null, null);
        }

        private string GetValue(string controlName)
        {
            foreach (Control item in this.Controls)
            {
                if (item.Name == controlName)
                {
                    return item.Text;
                }
            }
            return "0";
        }

        private void SetValue(string controlName, string value)
        {
            foreach (Control item in this.Controls)
            {
                if (item.Name == controlName)
                {
                    item.Text = value;
                    break;
                }
            }
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            try
            {
                using (SksEntity ent = new SksEntity())
                {
                    VergiTanim tnm;
                    for (int i = 1; i < 11; i++)
                    {
                        tnm = ent.VergiOranlari.Where(x => x.No == i).FirstOrDefault();
                        if (tnm != null)
                        {
                            tnm.Aciklama = GetValue("txtTanim" + i.ToString());
                            tnm.Oran = Cevir.Parse<decimal>(GetValue("txtOran" + i.ToString()));
                            tnm.Sira = Cevir.Parse<int>(GetValue("txtNo" + i.ToString()));
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(GetValue("txtTanim" + i.ToString())) && !string.IsNullOrEmpty(GetValue("txtOran" + i.ToString())) && !string.IsNullOrEmpty(GetValue("txtNo" + i.ToString())))
                            {
                                tnm = new VergiTanim();
                                tnm.No = (byte)i;
                                tnm.Aciklama = GetValue("txtTanim" + i.ToString());
                                tnm.Oran = Cevir.Parse<decimal>(GetValue("txtOran" + i.ToString()));
                                tnm.Sira = Cevir.Parse<int>(GetValue("txtNo" + i.ToString()));
                                ent.VergiOranlari.Add(tnm);
                            }
                        }
                    }
                    ent.SaveChanges();
                }
                MsgBox.Show(MessageStyle.Ok, "Uyarı", "Kaydedildi !");
            }
            catch (Exception)
            {

            }
        }
    }
}
